package edu.gduf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gduf.dao.MessageDao;
import edu.gduf.domain.Message;
import edu.gduf.domain.ResultInfo;
import edu.gduf.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class MessageServiceImpl implements MessageService {

    /**
     * 声明一页的大小为6
     */
    private static final Integer limit = 6;
    @Autowired
    private MessageDao messageDao;

    /**
     * 获取私信列表
     * @param page  页面下标
     * @param owner 消息的所属者
     * @return
     */
    @Override
    public PageInfo<Message> getMessageList(Integer page, String owner) {
        PageHelper.startPage(page, limit); // 设置页面的下标以及一个页面显示的数量
        List<Message> messageList = messageDao.getMessageList(owner); // 获取消息列表
        PageInfo<Message> pageInfo = new PageInfo<>(messageList); // 将消息列表加入PageInfo中
        return pageInfo;
    }

    /**
     * 查看对话详情
     * @param page       页面下标
     * @param owner      消息所属者
     * @param friendName 关联的用户名
     * @return
     */
    @Override
    public PageInfo viewMessageDetail(Integer page, String owner, String friendName) {
        PageHelper.startPage(page, limit);
        List<Message> messageList = messageDao.getAllMessageByOF(owner, friendName);
        PageInfo pageInfo = new PageInfo(messageList);
        List<Message> list = pageInfo.getList();
        List<Integer> midlist = new ArrayList<>();
        for (Message l:list){
            if(l.getStatus() == 1)
                midlist.add(l.getMid());
        }
        if(!midlist.isEmpty())
            messageDao.readMessage(midlist);
        return pageInfo;
    }

    /**
     * 发送私信
     * @param message
     * @return
     */
    @Override
    public ResultInfo sendMessage(Message message) {
        String owner = message.getOwner(); // 记录发送者名字
        String friendName = message.getFriendName(); // 记录接收者名字
        Message message1 = (Message) message.clone(); // 对象克隆
        message1.setStatus(1); //设置成未读状态
        message1.setOwner(friendName); // 将本来发送者名字设置成接收者
        message1.setFriendName(owner); //  将本来接收者名字设置成发送者
        int res1 = messageDao.addMessage(message);
        int res2 = messageDao.addMessage(message1);
        if (res1 == 1 && res2 == 1)
            return ResultInfo.successInfo("发送成功");
        return ResultInfo.failInfo("发送失败");
    }

    @Override
    public ResultInfo deleteMessageByMid(Integer mid) {
        List<Integer> list = new ArrayList<>();
        list.add(mid);
        int res = messageDao.deleteMessageByMid(list);
        if (res >= 1)
            return ResultInfo.successInfo("删除成功");
        return ResultInfo.failInfo("删除失败");
    }

    @Override
    public ResultInfo deleteAllMessageByOF(String owner, String friendName) {
        int res = messageDao.deleteAllMessageByOF(owner, friendName);
        if (res >= 1)
            return ResultInfo.successInfo("删除成功");
        return ResultInfo.failInfo("删除失败");
    }

    @Override
    public ResultInfo deleteSelectedMessageByMid(List<Integer> messageArray) {
        int res = messageDao.deleteMessageByMid(messageArray);
        if(res >=1)
            return ResultInfo.successInfo("删除成功");
        return ResultInfo.failInfo("删除失败");
    }

    /**
     * 获取未读私信数量
     * @param owner
     * @return
     */
    @Override
    public Integer getUnreadNum(String owner) {
        return messageDao.getUnReadNumByOwner(owner);
    }

    @Override
    public Integer getAllMessageNum(String owner) {
        return messageDao.getAllMessageNumByOwner(owner);
    }

    /**
     * 获取与某人的私信数量
     * @param owner
     * @param friendName
     * @return
     */
    @Override
    public Integer getMessageNumWithFriend(String owner, String friendName) {
        return messageDao.getMessageNumWithFriend(owner,friendName);
    }
}
